<?php
/*
 * @Create By: Vscode
 * @Author: falcon
 * @Date: 2019-05-03 08:40:06
 * @星爱后台
 */


use Workerman\Worker;
use application\depend\Regular;
use application\depend\Appresult;

require_once __DIR__ . '/Workerman/Autoloader.php';
require_once __DIR__ . '/config/errcode.php';
require_once __DIR__ . '/config/debug.php';
require_once __DIR__ . '/application/common/base.php';


define('APP_LOG_PATH_NEW', __DIR__ . '/runtime');
define('APP_START_ROOT_PATH', __DIR__);   //根目录
define('MAX_REQUEST', 1000);// 最大请求量

// 创建一个Worker监听端口，使用http协议通讯
$http_worker = new Worker("http://0.0.0.0:" . WEB_SERVER_PORT);
//服务名称
$http_worker->name = "xingai-webserver";
// 启动4个进程对外提供服务
$http_worker->count = WEB_SERVER_PROCESS_NUM;
if (!is_dir(APP_LOG_PATH_NEW)) {
    @mkdir(APP_LOG_PATH_NEW);
}
Worker::$logFile = APP_LOG_PATH_NEW . '/workerman.log';
Worker::$stdoutFile = APP_LOG_PATH_NEW . '/std_' . date("Y-m-d_H");

// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function ($connection, $data) {
    static $request_count = 0;
    // 向浏览器发送hello world
    if (isset($_SERVER['REQUEST_URI'])) {
        $reqPath = $_SERVER['REQUEST_URI'];
        if (Regular::checkPathInfo($reqPath)) {
            $reqPath = rtrim($reqPath, "/");
            switch ($reqPath) {
                case "/phptest/kafka":  //kafka 测试
                    $obj = new \application\controller\Kafka();
                    $msg = $obj->main();
                    break;

                case "/delete/repeat":
                    $obj = new \application\controller\delrepeat();
                    $msg = $obj->run();
                    break;

                case "/orderstatus/sync":
                    $obj = new \application\controller\OrderStatusSync();
                    $msg = $obj->main();
                    break;

                    case "/modify/ordersalesman":
                        $obj = new \application\controller\Fix();
                        $msg = $obj->main();
                        break;
           
                default:
                    $msg = [
                        'oh' => 'zero kill[uo]'
                    ];
                    break;
            }

        } else {
            $msg = [
                'hello' => 'org!'
            ];
        }//
    } else {
        $msg = [
            'aaa' => 'done',
        ];
    }

    if (is_object($msg)) {
        $msg = (array)$msg;
        if (!isset($msg['total']) || null === $msg['total']) {
            unset($msg['total']);
        }
    }
    $msg = json_encode($msg, JSON_UNESCAPED_UNICODE);
    if (null == $msg) {
        $result = new Appresult();
        $result->result = ERR_DB_DATA_EMPTY;
        $result->msg = "返回结果失败咯！";
        $connection->send(json_encode($result, JSON_UNESCAPED_UNICODE));
    } else {
        $connection->send($msg);
    }//end if
    if (++$request_count >= MAX_REQUEST) {
        Worker::stopAll();
    }//end if
};
// require_once __DIR__ . '/timer.php';
Worker::runAll();

